#/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2022/1/11 9:15
# @Author  : wyy
# @Email   : 1915059545@qq.com
# @File    : page_cphome.py
# @Software: PyCharm
"""产品主页"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from pages.base_page import BasePage

class PageCpHome(BasePage):
    # 产品主页url地址
    cp_url = "http://192.168.1.30/crm/index.php?m=product"
    # 主页产品按钮
    cp_locator = (By.CSS_SELECTOR,
                  "body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li:nth-child(4) > a")
    # 添加产品按钮
    tjcp_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div:nth-child(2) > div.pull-right > a")
    # 删除按钮
    delete_cp_locator = (By.ID, "delete")
    # 选择字段下拉框
    ryzd_locator = (By.CSS_SELECTOR, "#field")
    # 搜索的第二个下拉框
    dexlk_locator = (By.ID, "condition")
    # 搜索输入框
    sssrk_locator = (By.ID, "search")
    # 搜索按钮
    ss_locator = (By.XPATH, '//*[@id="dosearch"]')
    # 编辑按钮
    bianji_locator = (By.CSS_SELECTOR, "#form1 > table > tbody > tr:nth-child(1) > td:nth-child(6) > a:nth-child(2)")
    # 编辑产品名称
    cpmc_locator = (By.NAME, "name")
    bc_locator = (By.NAME, "submit")

    def bj_click(self):
        self.driver.find_element(*self.bianji_locator).click()
    def cpmc_input(self,cpmc):
        self.driver.find_element(*self.cpmc_locator).send_keys(cpmc)
# 点击产品按钮
    def cp_click(self):
        self.driver.find_element(*self.cp_locator).click()
#点击添加产品
    def cp_add_click(self):
        self.driver.find_element(*self.tjcp_locator).click()
#点击删除
    def cp_delete_click(self):
        self.driver.find_element(*self.delete_cp_locator).click()
#添加断言
    def cp_duanyan(self):
        dy_locator=(By.XPATH,'//*[@id="form1"]/table/tbody/tr[1]/td[3]/a/span')
        return self.driver.find_element(*dy_locator).text
#删除断言
    def sccp_duanyan(self):
        dy_locator1=(By.XPATH,'//*[@id="form1"]/table/tbody/tr[1]/td[3]/a/span')
        return self.driver.find_element(*dy_locator1).text
#搜索断言
    def ss_duanyan(self):
        dy_locator2 = (By.XPATH, '//*[@id="form1"]/table/tbody/tr/td')
        return self.driver.find_element(*dy_locator2).text
#断言删除多个
    def cp_duanyan_dg(self):
        dy_locator=(By.XPATH,'//*[@id="form1"]/table/tbody/tr[1]/td[3]/a/span')
        dy_locator1=(By.XPATH, '//*[@id="form1"]/table/tbody/tr[2]/td[3]/a/span')
        return self.driver.find_element(*dy_locator,*dy_locator1).text
#选择搜索条件,点击搜索
    def  cp_sousuo(self,index1,index2,neirong):
        # 选择搜索条件,第一个下拉框
        self.ryzd_select_element=self.driver.find_element(*self.ryzd_locator)
        self.ryzd_select=Select(self.ryzd_select_element)
        self.ryzd_select.select_by_index(index1)
        #选择搜索条件，第二个下拉框：
        self.dexlk_locator_element = self.driver.find_element(*self.dexlk_locator)
        self.dexlk_select = Select(self.dexlk_locator_element)
        self.dexlk_select.select_by_index(index2)
        #输入搜索内容
        self.driver.find_element(*self.sssrk_locator).send_keys(neirong)
        self.driver.find_element(*self.ss_locator).click()
    def ss_click(self):
        self.driver.find_element(*self.ss_locator).click()
#选择产品勾选条件,并删除(单个)
    def delete_gouxuan_dange(self,cpmc):
        # 定位产品列表
        cp_table_loccator = (By.XPATH, '//*[@id="form1"]/table/tbody')
        table = self.driver.find_element(*cp_table_loccator)
        trs = table.find_elements(By.TAG_NAME, "tr")
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, "td")
            print(tds[2].text)
            if tds[2].text == (cpmc):
                tds[0].find_element(By.TAG_NAME, "input").click()
                break
        # 删除
        sc_locator = (By.ID, "delete")
        self.driver.find_element(*sc_locator).click()
        self.driver.switch_to.alert.text
        self.driver.switch_to.alert.accept()
#选择产品勾选条件，删除（多个产品）
    def delete_gouxuan_duoge(self, cpmc1,cpmc2):
        cp_table_loccator = (By.XPATH, '//*[@id="form1"]/table/tbody')
        table = self.driver.find_element(*cp_table_loccator)
        trs = table.find_elements(By.TAG_NAME, "tr")
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, "td")
            print(tds[2].text)
            if tds[2].text == (cpmc1):
                tds[0].find_element(By.TAG_NAME, "input").click()
                break
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, "td")
            print(tds[2].text)
            if tds[2].text == (cpmc2):
                tds[0].find_element(By.TAG_NAME, "input").click()
                break
        # 删除
        sc_locator = (By.ID, "delete")
        self.driver.find_element(*sc_locator).click()
        self.driver.switch_to.alert.text
        self.driver.switch_to.alert.accept()
    #点击保存
    def click_bc(self):
        self.driver.find_element(*self.bc_locator).click()

    # 点击进入产品主页，点击进入添加产品页面
    def jr_cp_tj(self):
        self.cp_click()
        self.cp_add_click()

    #点击进入产品主页，点击进入编辑产品页面，编辑功能
    def bianji(self,cpmc,gouxuan):
        self.bj_click()
        self.cpmc_input("")
        self.click_bc()
        self.delete_gouxuan_dange("")
